package adapter;

import android.content.Context;
import android.graphics.Color;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.ArrayList;

import bean.Oil;
import zhang.automobileservice.R;

/**
 * Created by Administrator on 2016/7/18.
 */
public class DiscoverGridviewAdapter extends BaseAdapter {
    private Context context;
    private ArrayList<Oil> arrayList;

    public DiscoverGridviewAdapter(Context context, ArrayList<Oil> arrayList) {
        this.context = context;
        this.arrayList = arrayList;
    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.gridview_item,null);
            viewHolder.tv_oilnum = (TextView) convertView.findViewById(R.id.tv_oilnum);
            viewHolder.tv_oilkark = (TextView) convertView.findViewById(R.id.tv_oilmark);
            viewHolder.tv_oilprice = (TextView) convertView.findViewById(R.id.tv_oilprice);
            convertView.setTag(viewHolder);
        }else{
           viewHolder = (ViewHolder) convertView.getTag();
        }
        viewHolder.tv_oilnum.setText(Html.fromHtml(arrayList.get(position).getOilNum()+"<sup>#</sup>"));
        viewHolder.tv_oilprice.setText(arrayList.get(position).getOilPrice());
        switch (position){
            case 0:
                viewHolder.tv_oilkark.setBackgroundColor(context.getResources().getColor(R.color.grid0));
                break;
            case 1:
                viewHolder.tv_oilkark.setBackgroundColor(context.getResources().getColor(R.color.grid1));
                break;
            case 2:
                viewHolder.tv_oilkark.setBackgroundColor(context.getResources().getColor(R.color.grid2));
                break;
            case 3:
                viewHolder.tv_oilkark.setBackgroundColor(context.getResources().getColor(R.color.grid3));
                break;
        }
        return convertView;
    }
    static class ViewHolder {
        TextView tv_oilnum,tv_oilprice,tv_oilkark;
    }
}
